﻿using System;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml.Linq;
using Eagleshoes.PackingBarcode.Properties;

namespace Eagleshoes.PackingBarcode.Shipping
{
    public partial class DeliveryScanForm : Form
    {
        public DeliveryScanForm()
        {
            InitializeComponent();
        }


        private readonly Scanner _scanner = new Scanner();
        private XElement _doc =new XElement("root");
        private string _savePath;

        private void DeliveryScanForm_Load(object sender, EventArgs e)
        {
            string scanedDirPath = Program.DeliveryScan + "\\" + DateTime.Today.ToString("yyyy-MM-dd");
            if (!Directory.Exists(scanedDirPath))
            {
                Directory.CreateDirectory(scanedDirPath);
            }

            _savePath = string.Format(@"{0}\{1}.xml", scanedDirPath,DateTime.Now.ToString("HHmmss"));

            _scanner.DecodeEvent += OnScannerOnDecodeEvent;
            _scanner.Start();
            //BarcodeTextBox.Focus();
        }

        private void OnScannerOnDecodeEvent(object s, DecodeEventArgs ev)
        {
            BeginInvoke((Action<string>) delegate(string barcode)
                {
                    BarcodeTextBox.Text = barcode;
                    Scan(barcode);
                }, ev.Barcode.Trim('\0', '\t', '\n', '\r'));
            
        }

      

        public void Scan(string barcode)
        {
            _scanner.Stop();
            BarcodeTextBox.Text = barcode;
            InnerBoxTextbox.Text += barcode + "\r\n";
            _doc.Add(new XElement("carton",barcode));
            _doc.Save(_savePath);
            Win32.sndPlaySound(Resources.叮, Win32.SND_ASYNC | Win32.SND_MEMORY);
            _scanner.Start();
        }

        private void Reset()
        {
            _doc.Save(_savePath);

            BarcodeTextBox.Text = string.Empty;
            InnerBoxTextbox.Text = string.Empty;

            _doc=new XElement("root");
            _savePath = string.Format(@"{0}\{1}\{2}.xml", Program.DeliveryScan , DateTime.Today.ToString("yyyy-MM-dd"), DateTime.Now.ToString("HHmmss"));
          
            _scanner.Stop();
            _scanner.Start();
        }

        private void DeliveryScanForm_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F9:
                    Reset();
                    break;
                case Keys.F10:
                case Keys.Escape:
                    Close();
                    break;
                default:
                    break;
            }
        }

        private void DeliveryScanForm_Closing(object sender, CancelEventArgs e)
        {
            _scanner.DecodeEvent -= OnScannerOnDecodeEvent;
            _scanner.Stop();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Reset();
        }
    }
}